/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 1/22/2010
	Depreciated since s142 on 6/17/2011
*/

trigger AccManagerChangeOwnerInOpportunity on Opportunity (after insert, after update) {
	/*
	String curOppManager, newOppManager; // The name of the users with the Oppess and being granted Oppess
	ID curOppManagerID, curOppId, AdvancedAdSolutions_RecordTypeId; // The ID of the user with the Oppess
	OpportunityShare OppShr; // Opportunity Share object
	List<User> lUser = new List<User>(); // all Opportunity manager user list
	List<Opportunity> lNewOpp = new List<Opportunity>(); // all Opportunity records need to be shared
	List<OpportunityShare> lOppShrs = new List<OpportunityShare>();// Create a list of all OpportunityShare objects 
	List<OpportunityShare> lOldOppShrs = new List<OpportunityShare>();// Create a list of OpportunityShare objects to be deleted
	List<OpportunityShare> lNewOppShrs = new List<OpportunityShare>();// Create a list of OpportunityShare objects to be inserted.
	Set<ID> sCurOppManagerId = new Set<ID>(); // set of old Opportunity manager Ids
	Set<String> sNewOppManagerName = new Set<String>(); // set of new Opportunity manager names
	Map<String, ID> mOppManagerIdbyName = new Map<String, ID>(); // map Opportunity manager user Id by ccount manager user name
	Map<ID, String> mCurOppManagerName = new Map<ID, String>(); // all current Opportunity Manager Name by Opportunity Id
	if(Trigger.isAfter){
		if(AdvancedAdSolutions_RecordTypeId == null)
			AdvancedAdSolutions_RecordTypeId = RecordTypeHelper.GetRecordTypeID('Opportunity', 'Advanced Ad Solutions');
		
		if(Trigger.isUpdate){
			for(Opportunity a_update : Trigger.new){
				if(a_update.RecordTypeId == AdvancedAdSolutions_RecordTypeId){
					curOppManager = Trigger.oldMap.get(a_update.Id).Account_Manager__c;
					newOppManager = a_update.Account_Manager__c;
					if(curOppManager != null && newOppManager != null && curOppManager != newOppManager){// if Opportunity Mananger changed
						mCurOppManagerName.put(a_update.Id, curOppManager); // old Oppcount Manager manual shared rules need to be deleted
						newOppManager = a_update.Account_Manager__c;	
						sNewOppManagerName.add(newOppManager); // get all Opportunity Manager Name list
						lNewOpp.add(a_update); // get all Opportunity records list
					}
				}
			}
		}
		if(Trigger.isInsert){
			for(Opportunity a : Trigger.new){
				if(a.RecordTypeId == AdvancedAdSolutions_RecordTypeId){
					if(a.Account_Manager__c != null){	
						newOppManager = a.Account_Manager__c;	
						sNewOppManagerName.add(newOppManager); // get all Opportunity Manager Name list
						lNewOpp.add(a); // get all Opportunity records list
					}
				}
			}
		}
		if(!lNewOpp.isEmpty()){ // if mCurOppManagerName is not empty, lNewOpp must not be empty as well, so just check lNewOpp
			lUser = [select u.Id, u.Name from User u 
				     where u.IsActive = true and (u.Name In: mCurOppManagerName.values() or u.Name In: sNewOppManagerName)
				     Order By u.Name]; // get the information of all users being granted Oppess
			for(User u : lUser){
				mOppManagerIdbyName.put(u.Name, u.Id);
			}
			// delete old Opportunity Share
			if(!mCurOppManagerName.isEmpty()){
				for(String oldname : mCurOppManagerName.values()){
					sCurOppManagerId.add(mOppManagerIdbyName.get(oldname)); // get old Opportunity Manager Id set
				}
				lOppShrs = [select Id, OpportunityId, UserOrGroupId from OpportunityShare 
				              where OpportunityId In: mCurOppManagerName.keySet() and RowCause =: 'Manual' and UserOrGroupId In: sCurOppManagerId];//get all old Opportunityshare Id
				for(OpportunityShare OppShr_old : lOppShrs){
					curOppId = OppShr_old.OpportunityId;
					curOppManager = mCurOppManagerName.get(curOppId);
					curOppManagerID = mOppManagerIdbyName.get(curOppManager);
					if(OppShr_old.UserOrGroupId == curOppManagerID){
						lOldOppShrs.add(OppShr_old);
					}	
				}
				delete lOldOppShrs; // delet old Opportunity share for current Opportunity manager
			}			
			// insert/update new Opportunity Share
			for(Opportunity a_new : lNewOpp){
				OppShr = new OpportunityShare(); // create new Opportunity share object for Opportunity sharing
				OppShr.OpportunityId = a_new.Id;  // Oppound Id being shared
				OppShr.UserOrGroupId = mOppManagerIdbyName.get(a_new.Account_Manager__c); // user id being granted Oppess
				OppShr.OpportunityAccessLevel = 'Read'; //Read Oppess 
				lNewOppShrs.add(OppShr); // add to Opportunity share list
			}
			try {
				Database.SaveResult[] lsr = Database.insert(lNewOppShrs,false); // Insert the new sharing records and capture the save result.
				for(Database.SaveResult sr : lsr){// Process the save results for insert.
					if(!sr.isSuccess()){
						Database.Error err = sr.getErrors()[0]; // Get the first save result error.
						System.debug('err.getMessage() = ' + err.getMessage());
						System.debug('err.getStatusCode() = ' + err.getStatusCode());
						if(!(err.getStatusCode() == StatusCode.FIELD_INTEGRITY_EXCEPTION
						&& err.getMessage().contains('OppessLevel'))){ // Error is not related to trivial Oppess level.
						}
					}
				}
			} catch(DmlException e) {
				System.debug('The Apex sharing recalculation threw the following exception: ' + e.getMessage());
			}
		}
	}	
	*/
}